<?php

if(!isset($_SESSION['isLogged']) || $_SESSION['isLogged'] != true) header('Location:../index.php');

//LOGOUT FUNCTION
if(isset($_GET['logout'])) {
	session_destroy();
	header('Location:index.php');
	exit;
}

include 'common.php';


//GET ITEM FOR CHANGE AND REMOVE ACTIONS
if (isset($_GET['item'])) $selectedItem = (int)htmlspecialchars($_GET['item']);


if($_POST){
    //DATA NORMALIZATION
    $new_bg_name = htmlspecialchars(addslashes($_POST['bg_name'] ));
    $new_price = floatval((htmlspecialchars(trim($_POST['price']))));
    $new_sizes = strip_tags($_POST['sizes'] );
    $new_descr = strip_tags($_POST['descr'] );

    if ($_POST['m_f'] == 'f') {
        $arrayHolder = $pics_women;
        $path_all = str_replace("../","",$path_txt_w);
        $path_pic_big = $path_big_w;
        $path_pic_small = $path_small_w;
    } else {
        $arrayHolder = $pics_men;
        $path_all = str_replace("../","",$path_txt);
        $path_pic_big = $path_big;
        $path_pic_small = $path_small;
    }
    if( count($_FILES) > 0 && isset($_FILES['snimka']) ) {
        $new_en_name = count($arrayHolder).pathinfo($_FILES['snimka']['name'],PATHINFO_FILENAME);
        $path_all .= $new_en_name.'.txt';
    }

    //GET INFO TO WRITE IN NEW TXT FILE
    $new_result = $new_bg_name."\r\n".$new_descr."\r\n".$new_price."\r\n".$new_sizes;
    $new_result = preg_replace('~\r[\n]?~', "\n", $new_result);


    //IMAGE RESIZE IN PHP WITH GD Library
    include('includes/pic_resize.php');
    $image = new SimpleImage();
    $image->load($_FILES['snimka']['tmp_name']);
    $image->resize(156,270);
    $image->save($path_pic_small.$new_en_name.'.'.pathinfo($_FILES['snimka']['name'],PATHINFO_EXTENSION));


    //CHECK IF WRITE FILES SUCCESSFUL AND GIVE MESSAGE TO USER
    if(file_put_contents($path_all, "\xEF\xBB\xBF".  $new_result)) //to write in UTF-8
    {
        if( move_uploaded_file($_FILES['snimka']['tmp_name'],
            $path_pic_big.$new_en_name.'.'.pathinfo($_FILES['snimka']['name'],PATHINFO_EXTENSION)) ) {
            echo '<br/><br/>Файловете са качени успешно.
            <a href="admin.php">Натиснете тук за да презаредите страницата.</a>';
        }
        else { echo 'Грешка при ъплоада на снимката.'; }
    }
    else { echo 'Грешка при ъплоада на текстовия файл.'; }

}

?>


    <br/>
	<form method="GET">
		Логаут от админ системата:<input type="submit" value="Логаут" name="logout"/>
	</form>
	<hr>

	<form method="POST" enctype="multipart/form-data">
		<h3>Система за ъплоад на нови тениски: </h3><br/>
        Име на тениската:<input type="text" name="bg_name" size="40"/><br/>
        Мъже/Жени:
        <select name="m_f">
            <option value="m">Мъже</option>
            <option value="f">Жени</option>
        </select><br/>
        Цена:<input type="text" name="price"/><br/>
        Размери:<input type="text" name="sizes"/>(сложи в следния формат: <span style="color:blue;">S1,M12,L2</span>
        където цифрата е бройката за съответния размер)<br/>
		Път до снимката:<input type="file" name="snimka"/>(Силно препоръчително е снимката да е с размери 520x900 пискела)<br/>
        Описание:<textarea name="descr" rows="10" cols="40"></textarea>
		<input type="submit" name="add" value="Качи"/>
	</form>
	<hr>

	<h3> Списък на текущите тениски въведени в базата данни:</h3><br/>

	<!-- ZAGLAVEN RED NA TABLICATA -->
	<div>
		<span class="topline" style="width:310px; background-color: #ff5f69; text-align: center; display:inline-block;">Име на файла</span>
		<span class="topline" style="width:95px; background-color: #ff5f69; text-align: center;  display:inline-block;">Мъже/Жени</span>
        <span class="topline" style="width:230px; background-color: #ff5f69; text-align: center;  display:inline-block;">Име на тениската</span>
        <span class="topline" style="width:45px; background-color: #ff5f69; text-align: center;  display:inline-block;">Цена</span>
        <span class="topline" style="width:80px; background-color: #ff5f69; text-align: center;  display:inline-block;">Размери</span>
        <span class="topline" style="width:70px; background-color: #ff5f69; text-align: center;  display:inline-block;">Промени</span>
        <span class="topline" style="width:70px; background-color: #ff5f69; text-align: center;  display:inline-block;">Изтрий</span>
	</div>

<?php

//IZVEJDANE NA SUDURJANIETO NA PAPKATA NA POTREBITELQ - MUJE I JENI
//echo "<pre>".print_r($pics_men, true)."</pre>";
echo '<br/>';
foreach ($pics_men as $key => $value) {
    static $i = 1;
    if ( ($i % 2) == 0 ) echo '<div style="border: 1px solid black; font-size: 15px; background-color: #ccc;">';
        else echo '<div style="border: 1px solid black; font-size: 15px; ">';

	echo '<span class="admin_line admin_pics" style="margin-bottom: 5px; width:310px; display:inline-block;"><a href="'.$pics_men[$key]['loc_big'].'">'.$pics_men[$key]['en_name'].'</a></span>
			<span class="admin_line" style="width:95px; display:inline-block;">m</span>
			<span class="admin_line" style="width:230px; display:inline-block;">'.$pics_men[$key]['bg_name'].'</span>
			<span class="admin_line" style="width:45px; display:inline-block;">'.$pics_men[$key]['price'].'</span>
			<span class="admin_line" style="width:80px; display:inline-block;">'.$pics_men[$key]['sizes'].'</span>
			<span class="admin_line" style="width:70px; display:inline-block;"><a href="admin_item_details.php?item='.
            $key.'">Промени</a></span>
			<span class="admin_line file_delete" style="width:70px; display:inline-block;"><a href="admin_item_delete.php?item='.
            $key.'">Изтрий</a></span>
		</div>';
    $i++;
}

echo '<br/>';

foreach ($pics_women as $key => $value) {
    static $i = 1;
    if ( ($i % 2) == 0 ) echo '<div style="border: 1px solid black; font-size: 15px; background-color: #ccc;">';
    else echo '<div style="border: 1px solid black; font-size: 15px; ">';

    echo '<span class="admin_line admin_pics" style="margin-bottom: 5px; width:310px; display:inline-block;"><a href="'.$pics_women[$key]['loc_big'].'">'.$pics_women[$key]['en_name'].'</a></span>
			<span class="admin_line" style="width:95px; display:inline-block;">f</span>
			<span class="admin_line" style="width:230px; display:inline-block;">'.$pics_women[$key]['bg_name'].'</span>
			<span class="admin_line" style="width:45px; display:inline-block;">'.$pics_women[$key]['price'].'</span>
			<span class="admin_line" style="width:80px; display:inline-block;">'.$pics_women[$key]['sizes'].'</span>
			<span class="admin_line" style="width:70px; display:inline-block;"><a href="admin_item_details.php?item='.
        $key.'">Промени</a></span>
			<span class="admin_line file_delete" style="width:70px; display:inline-block;"><a href="admin_item_delete.php?item='.
        $key.'">Изтрий</a></span>
		</div>';
    $i++;
}

echo '<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>';


?>